<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+


class KBClientModel_common extends KBClientModel
{
	
	function _getCategoryListSql($parent_id) {
	
		$sort = $this->getSetting('category_sort_order');
		
		$sql = "
		SELECT 
			c.*,
			COUNT(DISTINCT ec.category_id) AS num_entries
		FROM 
			{$this->tbl->category} c
		LEFT JOIN {$this->tbl->entry_to_category} ec ON c.id = ec.category_id 
		
		WHERE 1
			AND c.active = 1
		GROUP BY c.id
		ORDER BY {$sort}";
		
		echo "<pre>"; print_r($sql); echo "</pre>";
		return $sql;
	}


	function &getCategoryList($parent_id) {
		
		$needed_keys = array('id', 'name');
		$data = array();
		
		$sql = $this->_getCategoryListSql($parent_id);
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		while($row = $result->FetchRow()) {
		
			//if($row['num_entries'] == 0) { continue; }
			$data[$row['id']] = array_diff_key($row, $needed_keys);
		}

		//echo "<pre>"; print_r($data); echo "</pre>";
		return $data;
	}
	
	
	function &getEntryList($body_words = 'all') {
		
		$needed_keys = array('id', 'title', 'body');
		$data = array();
		
		$sort = $this->getSetting('category_sort_order');
		$this->setSqlParamsOrder('ORDER BY cat.' . $sort);
		
		$sql = $this->getEntriesSql($body_words);
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		while($row = $result->FetchRow()) {
		
			$data[$row['category_id']][] = array_diff_key($row, $needed_keys);
		}

		//echo "<pre>"; print_r($data); echo "</pre>";
		return $data;
	}
}
?>